var swiper = new Swiper('.swiper .swiper-container', {
  spaceBetween: 30,
  centeredSlides: true,
  autoplay: {
    delay: 2500,
    disableOnInteraction: false,
  },
  pagination: {
    el: '.swiper-pagination',
    clickable: true,
  },
  navigation: {
    nextEl: '.swiper-button-next',
    prevEl: '.swiper-button-prev',
  },
});

var swiper1 = new Swiper('.swiper1 .swiper-container', {
  slidesPerView: 4,
  spaceBetween: 30,
  slidesPerGroup: 4,
  loop: true,
  loopFillGroupWithBlank: true,
  pagination: {
    el: '.swiper-pagination',
    clickable: true,
  },
  navigation: {
    nextEl: '.swiper-button-next',
    prevEl: '.swiper-button-prev',
  },
});
let search = document.querySelector('.swiper .swiper-container .swiper-wrapper');
pAjax({
    url:'./json/Bigimg.json'
}).then(function(res){
    res = JSON.parse(res);
    Bigimgs(res);
})
function Bigimgs(res){
    res.forEach((item, index) => {
      search.innerHTML  += `<div class="swiper-slide"><img src="${item.Bigimg}"/></div>`;
    });
}
let search1 = document.querySelector('.swiper1 .swiper-container .swiper-wrapper');
pAjax({
    url:'./json/Smallimg.json'
}).then(function(res){
    res = JSON.parse(res);
    Smallimgs(res);
})
function Smallimgs(res){
    res.forEach((item, index) => {
      search1.innerHTML  += `<div class="swiper-slide">
      <a href="./html/detail.html?id=${item.id}"><img src="${item.Smallimg}"/></a>
      <p>${item.title}</p>
      <p><span>￥${item.plice}</span><span>￥${item.oldPlice}</span></p>
  </div>`;
    });
}
// 京东秒杀
let flashSale = document.querySelector('.flashSale');
let time1 = document.querySelector('.flashSale .span1');
let time2 = document.querySelectorAll('.flashSale .span2')
flashSale.onclick = function(){
  location.href="https://miaosha.jd.com/"
}

let sjs = parseInt(Math.random() * 25);
var date = new Date;
var years = date.getFullYear();
var month = date.getMonth() + 1;//获取月
var d = date.getDate();
var h = date.getHours();//获取当前小时数(0-23)
var m = date.getMinutes();//获取当前分钟数(0-59)
var s = date.getSeconds();

var inputTime = sjs > h ? +new Date(`${years}-${month}-${d} ${sjs}:00:00`)
  : +new Date(`${years}-${month+1}-${d} ${sjs}:00:00`)
  if(sjs < 10 ){  
    sjs = '0' + sjs;  
  }
  time1.innerHTML = sjs + ':00点场'
countDown();
setInterval(countDown,1000)
function countDown (){
  var nowTime=+new Date(); //返回的是当前时间的总的毫秒数
  var times=(inputTime - nowTime)/1000;// times是剩余时间的总的秒数
  var h1=parseInt(times/60/60%24);
  var m1=parseInt(times/60%60);
  var s1=parseInt(times%60);
  h1 = h1 < 10 ? '0'+ h1 : h1;
  time2[0].innerHTML = h1;
  m1 = m1 < 10 ? '0'+ m1 : m1;
  time2[1].innerHTML = m1;
  s1 = s1 < 10 ? '0'+ s1 : s1;
  time2[2].innerHTML = s1;
}

// 今日特价
// let preferentials = document.querySelector('.preferential');
// let data = document.querySelectorAll('.preferential .top a');
// console.log(data);
// pAjax({
//   url:'./json/preferential.json'
// }).then(function(res){
//   res = JSON.parse(res);
//   console.log(res);
// })

// 为你推荐
let box2 = document.querySelector('.box2');
pAjax({
  url:'./json/data.json'
}).then(function(res){
  res= JSON.parse(res)
  datas(res);
  let imgs = document.querySelectorAll('.box2 img');
  lazyload(imgs);
  //点击图片进行跳转到详情页，除了轮播图的图片
  for (let index = 0; index < imgs.length; index++) {
    imgs[index].onclick = function(){
      if(imgs[index].classList == imgs[index].getAttribute('class')){
        location.href=`./html/detail.html?id=${imgs[index].getAttribute('class')}`
      }
    } 
  }
  // 滚屏函数
	window.onscroll =function(){
		lazyload(imgs);
	}
})
// 懒加载实现
function lazyload(imgs){
  let h = window.innerHeight;
  let s = document.documentElement.scrollTop || document.body.scrollTop;
  for(let i=0;i<imgs.length;i++){
    if ((h+s)>imgs[i].offsetTop) {
        setTimeout(function(){
          var temp = new Image();      
          temp.src = imgs[i].getAttribute('data-src');
          temp.onload = function(){
          imgs[i].src = imgs[i].getAttribute('data-src')
          }
        },2000)
    }
  }
}

//渲染为你推荐
function datas(res){
  res.forEach(function(item,index){
    box2.innerHTML += `<div>
    <img data-src="${item.Smallimg}" class="${item.id}"/>
    <p>${item.title}</p>
    <span>￥${item.plice}</span>
</div>`
  })
}



// 登录注册
let denglu = document.querySelector('#denglu');
let zhuce = document.querySelector('#zhuce');

function getUrlParam(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); // 构造一个含有目标参数的正则表达式对象
  var r = window.location.search.substr(1).match(reg); // 匹配目标参数
  if (r != null) return decodeURI(r[2]);
  return null;
}
let username = getUrlParam('username');
if(username){
  denglu.innerHTML = username;
  denglu.style.color = 'red'
  denglu.onclick = function(){
    location.href=" "
  
  }
}
denglu.onclick = function(){
  location.href="./html/login.html"

}
zhuce.onclick = function(){
  location.href="./html/reg.html"

}
